<?php
namespace app\web\controller;

use app\model\Theme; 
use app\model\Bargain; 

class LtBargain extends Base
{
    public function Lt_bargain()
    {
/**
 * Created by 底片云
 * Desc:抽奖机会
 * User:cbw
 * Date: 2020/11/29
 */
require IA_ROOT . '/addons/'.MODULE_NAME.'/function/common.func.php';
global $_G,$_P;
$params = $_P;
$themeid = isset($params['themeid'])?$params['themeid']:'';//活动id
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if (!$theme){
    $response = modelShow(40002);
    return_msg($response, '', 'ajax');
}
$config =   Theme::getConfig($themeid);
$system= $_G['system'];
$do = 'Lt_bargain';
$ac = !empty($params['ac'])?$params['ac']:'display';
$nickname = isset($params['nickname'])?trim($params['nickname']):'';
$start_day = isset($params['createtime']['start'])?$params['createtime']['start']:time();
$end_day = isset($params['createtime']['end'])?$params['createtime']['end']:time();
$order_id = isset($params['order_id'])?$params['order_id']:'';
$current_status  = isset($params['status'])?$params['status']:0;
$be_id  = isset($params['be_id'])?$params['be_id']:0;
$page_index = max(1,intval($params['page']));
$page_size = 10;
$page_start = ($page_index-1)*$page_size;
$lt_item_page_total = intval(db_find("select count(*) as cnt from".tablename(t_bargain_entry))['cnt']);
$type_options = Bargain::getBargainStatusList($themeid);
if ($ac=='display'){
    if ($current_status==0){
        $page_total = intval(db_find("select count(*) as cnt from ".tablename(t_bargain_entry))['cnt']);
    }else{
        $page_total = intval(db_find("select count(*) as cnt from ".tablename(t_bargain_entry)." where status=".$current_status)['cnt']);
    }
    $list = Bargain::getDefaultOrSearchBargainEntry($themeid,$nickname,'','','',$current_status,$page_start,$page_size);
    $pager = pagination($page_total,$page_index,$page_size);
}
if ($ac=='search'){
    if (is_int($order_id)){
        $nickname = '';
    }elseif (is_string($order_id)){
        $order_id = '';
    }
    if (!empty($nickname)){
        $nickname = '%'.$nickname.'%';
    }
    if ($start_day==$end_day){
        $list = Bargain::getDefaultOrSearchBargainEntry($themeid,$nickname,'','',$order_id,$current_status,$page_start,$page_size);
    }else{
        $list = Bargain::getDefaultOrSearchBargainEntry($themeid,$nickname,$start_day,$end_day,$order_id,$current_status,$page_start,$page_size);
    }
}
if ($ac=='detail'){
    $page_total = intval(db_find("select count(*) as cnt from ".tablename(t_bargain_flag)." where be_id=".$be_id)['cnt']);
    $list = Bargain::getBargainFlagList($themeid,$be_id,$page_start,$page_size);
    $pager = pagination($page_total,$page_index,$page_size);
}
if ($ac=='del'){
    $res = Bargain::delBargainEntryByBeid($themeid,$be_id);
}

$total = count($list);
$return_clicent = get_defined_vars();
return view('/lt_bargain', $return_clicent);
}
}